import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Preview
@Composable
fun MySideEffect1() {
    var count by remember { mutableStateOf(0) }
    SideEffect {
        Log.e("zh", "SideEffect触发${count}")
    }
    Text(text = count.toString(),fontSize = 36.sp, modifier = Modifier.clickable {
        count++
    })
}

@Composable
fun MySideEffect2() {
    var text by remember { mutableStateOf("Common") }
    Text(text = "text $text")
//    Thread.sleep(3 * 1000)
    text = "Delay text"
}

@Composable
fun MySideEffect3() {
    var count by remember { mutableStateOf(0) }
    Text(text = "text $count", modifier = Modifier
        .padding(20.dp)
        .clickable {
            count += 1
            println("clicked")
        })
    println("1 "+count)
//    SideEffect {
////        println("curr thread ${Thread.currentThread()}")
////        Thread.sleep(3 * 1000)
////        count+=100
//        println("2 "+count)
//    }
    count+=100
    count+=10000
    println("3 "+count)
}

@Composable
fun MySideEffect4() {
    // Define a state variable for the count
    val count = remember { mutableStateOf(0) }

    // Use SideEffect to log the current value of count
    SideEffect {
        // Called on every recomposition
        println("Count is ${count.value}")
    }

    Column {
        Button(onClick = { count.value++ }) {
            Text("Increase Count")
        }

        // With every state update, text is changed and recomposition is triggered
        Text("Counter ${count.value}")
    }
}

@Composable
fun MySideEffect5() {
    val count = remember { mutableStateOf(0) }

    SideEffect {
        println("Count is ${count.value}")
        println("MySideEffect5 scope SideEffect ")
    }
    test()

    Column {
        Button(onClick = { count.value++ }) {
            // This recomposition doesn't trigger the outer side effect
            // every time button has been tapped
            Text("Increase Count ${count.value}")
        }
    }
}

@Composable
private fun test() {
    Button(onClick = {  }) {
        Text(text = "test btn")
    }
    println("test()")
    SideEffect {
        println("test scope SideEffect ")
    }

}